//
//  LGAdviceRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/7.
//  医嘱记录

#import "LGAdviceRecordController.h"
#import "LGAdviceRecordDateHeader.h"
#import "LGAdviceRecordTitleHeader.h"
#import "LGAdviceRecordMedicineCell.h"
#import "LGAdviceRecordOtherCell.h"
@interface LGAdviceRecordController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
@end

@implementation LGAdviceRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor clearColor];
    self.levelid = (_levelid && _levelid.length>0)?_levelid:@"";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    self.emptyView.backgroundColor = [UIColor clearColor];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.followup_advice/advice" parameters:@{@"patient_id":self.patientid,@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            NSArray *tempArray = returnData;
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSInteger i=0; i<tempArray.count; i++) {
                NSDictionary *tempDic = tempArray[i];
                NSArray *medicineArray = tempDic[@"medicines"];
                NSArray *otherArray = tempDic[@"others"];
                NSMutableArray *resultArray = [NSMutableArray array];
                NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithDictionary:@{@"type":@"header",@"doctor_name":LGNSString(tempDic[@"doctor_name"]),@"advice_date":LGNSString(tempDic[@"advice_date"]),@"source":LGNSString(tempDic[@"source"]),@"source_type":LGNSString(tempDic[@"source_type"])}];
                
                for (NSInteger i=0; i<medicineArray.count; i++) {
                    NSMutableDictionary *medicineDic = [NSMutableDictionary dictionaryWithDictionary:medicineArray[i]];
                    [medicineDic setValue:@"medicine" forKey:@"cellType"];
                    [resultArray addObject:medicineDic];
                }
                
                for (NSInteger i=0; i<otherArray.count; i++) {
                    NSMutableDictionary *otherDic = [NSMutableDictionary dictionaryWithDictionary:otherArray[i]];
                    [otherDic setValue:@"other" forKey:@"cellType"];
                    [resultArray addObject:otherDic];
                }
                
                [dataDic setValue:resultArray forKey:@"dataArray"];
                [dataArray addObject:dataDic];
                
//                if(medicineArray.count>0){
//                    [dataDic setValue:@"用药医嘱" forKey:@"title"];
//                    [dataDic setValue:medicineArray forKey:@"dataArray"];
//                    [dataArray addObject:dataDic];
//                    if(otherArray.count>0){
//                        //两者都有
//                        NSMutableDictionary *dataDic2 = [NSMutableDictionary dictionaryWithDictionary:@{@"type":@"noheader",@"title":@"其他医嘱",@"doctor_name":LGNSString(tempDic[@"doctor_name"]),@"advice_date":LGNSString(tempDic[@"advice_date"]),@"source":LGNSString(tempDic[@"source"]),@"source_type":LGNSString(tempDic[@"source_type"])}];
//                        [dataDic2 setValue:otherArray forKey:@"dataArray"];
//                        [dataArray addObject:dataDic2];
//                    }
//                }else if (otherArray.count>0){
//                    [dataDic setValue:@"其他医嘱" forKey:@"title"];
//                    [dataDic setValue:otherArray forKey:@"dataArray"];
//                    [dataArray addObject:dataDic];
//                }
            }
            self.dataArray = dataArray;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无医嘱记录" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无医嘱记录" offset:0];
        }
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"dataArray"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *tempArray = self.dataArray[indexPath.section][@"dataArray"];
    NSDictionary *tempDic = tempArray[indexPath.row];
    if([LGNSString(tempDic[@"cellType"]) isEqualToString:@"medicine"]){
        //用药医嘱
        LGAdviceRecordMedicineCell *medicineCell = [tableView dequeueReusableCellWithIdentifier:@"medicineCell" forIndexPath:indexPath];
        medicineCell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(tempArray.count == 1){
            medicineCell.type = @"single";
        }else if (indexPath.row == 0){
            medicineCell.type = @"top";
        }else if (indexPath.row == tempArray.count-1){
            medicineCell.type = @"bottom";
        }else{
            medicineCell.type = @"middle";
        }
        medicineCell.sourceType = LGNSString(tempDic[@"source_type"]);
        medicineCell.dataDic = tempDic;
        return medicineCell;
    }else{
        //其他医嘱
        LGAdviceRecordOtherCell *otherCell = [tableView dequeueReusableCellWithIdentifier:@"otherCell" forIndexPath:indexPath];
        otherCell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(tempArray.count == 1){
            otherCell.type = @"single";
        }else if (indexPath.row == 0){
            otherCell.type = @"top";
        }else if (indexPath.row == tempArray.count-1){
            otherCell.type = @"bottom";
        }else{
            otherCell.type = @"middle";
        }
        otherCell.dataDic = tempDic;
        return otherCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(40);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGAdviceRecordDateHeader *dateHeader = [[LGAdviceRecordDateHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    dateHeader.dataDic = self.dataArray[section];
    return dateHeader;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(3);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(3))];
}


#pragma mark
#pragma mark ====> 创建控件
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height);
    self.emptyView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height);
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(44)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGAdviceRecordMedicineCell class] forCellReuseIdentifier:@"medicineCell"];
        [_tableView registerClass:[LGAdviceRecordOtherCell class] forCellReuseIdentifier:@"otherCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(46);
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
